
(* Dimensions d'une image *)
let get_dims img =
  ((Sdlvideo.surface_info img).Sdlvideo.w, (Sdlvideo.surface_info img).Sdlvideo.h)
 
(* init de SDL *)
let sdl_init () =
  begin
    Sdl.init [`EVERYTHING];
    Sdlevent.enable_events Sdlevent.all_events_mask;
  end
 
(* attendre une touche ... *)
let rec wait_key () =
  let e = Sdlevent.wait_event () in
    match e with
    Sdlevent.KEYDOWN _ -> ()
      | _ -> wait_key ()
 
(*
  show img dst
  affiche la surface img sur la surface de destination dst (normalement l'écran)
*)
let show img dst =
  let d = Sdlvideo.display_format img in
    Sdlvideo.blit_surface d dst ();
    Sdlvideo.flip dst
   
 let rec elt (i,l) = match (i,l) with
     (_,[]) -> failwith "depassement de limite"
   | (0,e::l) -> e
   | (_,e::l1) -> elt(i-1,l1)
       
(*parseur : prend les couleurs et hauteur et les met dans une liste*)
 (*let parse_hauteur file = 
   begin   
     let l1 = ref [] in
     let l2 = ref [] in  
     let i = ref (int_of_string(input_line file)) in
       while !i>0 do
	 l1 := (int_of_string(input_line file)
		 ,int_of_string(input_line file)
		   ,int_of_string(input_line file)) :: !l1; 
	 l2 := int_of_string(input_line file) :: !l2;
	 i := !i - 1;
	 (*insertion en tête élèment s inversés*)
       done;
       (!l1,!l2);
   end*)
 
let rec parse_hauteur l1 l2 = function
     [] -> (l1,l2)
   | (h,r,g,b)::l -> parse_hauteur ((r,g,b)::l1) (h::l2) l 

(*cree liste de couleur avec sa hauteur pour avoir la hauteur des couleurs*)
let height_of_color (r,g,b) fileparse =
   begin
     let listeh = snd(fileparse) in
       (*liste de couleurs*)
     let listec = fst(fileparse) in
     let h = ref 0 in
       for i = 0 to ((List.length listec)-1) do
	 if (r,g,b) = elt (i,listec) then
	   h := elt(i,listeh);
       done;      
       !h;	
   end 

(*cree liste de couleur avec sa hauteur pour avoir la hauteur des couleurs*)
(*let height_of_color (r,g,b) l=
   begin
     let fileopen = open_in "file_for_said.txt" in
     let fileparse = parse_hauteur fileopen in
       (*liste de hauteurs*)
     let listeh = snd(fileparse) in
       (*liste de couleurs*)
     let listec = fst(fileparse) in
     let h = ref 0 in
       for i = 0 to ((List.length listec)-1) do
	 if (r,g,b) = elt (i,listec) then
	   h := elt(i,listeh);
       done;
       close_in fileopen;       
       !h;	
   end *)
   
  let intersects img inter listecouleur=
   begin
     let liste = ref [] in
     let y = ref 0 in 
     let x = ref 0 in
     let fileparse = parse_hauteur [] [] listecouleur in
       while !y <= snd(get_dims img) - inter do
	 while !x <= fst(get_dims img) -inter do (*rajoute les 2 triangles d'un carre a la liste*)
	   if (((!y +inter) < snd(get_dims img)-1) && ((!x +inter) < fst(get_dims img)-1))then
	     begin 
	     liste := (!x,!y,height_of_color (Sdlvideo.get_pixel_color img !x !y) fileparse) ::
	          (!x+inter,!y,height_of_color (Sdlvideo.get_pixel_color img (!x+inter) !y) fileparse) ::
		  (!x+inter,!y+inter,height_of_color (Sdlvideo.get_pixel_color img (!x+inter) (!y+inter)) fileparse)::
	          (!x,!y,height_of_color (Sdlvideo.get_pixel_color img !x !y) fileparse) ::
		  (!x,!y+inter,height_of_color (Sdlvideo.get_pixel_color img !x (!y+inter)) fileparse) ::
		  (!x+inter,!y+inter,height_of_color (Sdlvideo.get_pixel_color img (!x+inter) (!y+inter)) fileparse) :: 
	          !liste;
	     x := !x + inter;
	     end
	   else
	     begin
	     if ((!x +inter) > fst(get_dims img)-1) then
	       begin
	       liste := (!x,!y,height_of_color (Sdlvideo.get_pixel_color img !x !y) fileparse) ::
	            (fst(get_dims img)-1,!y,height_of_color (Sdlvideo.get_pixel_color img (fst(get_dims img)-1) !y) fileparse) ::
		    (fst(get_dims img)-1,!y+inter,height_of_color (Sdlvideo.get_pixel_color img (fst(get_dims img)-1) (!y+inter)) fileparse)::
	            (!x,!y,height_of_color (Sdlvideo.get_pixel_color img !x !y) fileparse) ::
		    (!x,!y+inter,height_of_color (Sdlvideo.get_pixel_color img !x (!y+inter)) fileparse) ::
		    (fst(get_dims img)-1,!y+inter,height_of_color (Sdlvideo.get_pixel_color img (fst(get_dims img)-1) (!y+inter)) fileparse) :: 
	            !liste;
	       x := !x + inter;
	       end
	     else if ((!y +inter) > snd(get_dims img)-1) then
	       begin
	       liste := (!x,!y,height_of_color (Sdlvideo.get_pixel_color img !x !y) fileparse) ::
	            (!x+inter,!y,height_of_color (Sdlvideo.get_pixel_color img (!x+inter) !y) fileparse) ::
		    (!x+inter,snd(get_dims img)-1,height_of_color (Sdlvideo.get_pixel_color img (!x+inter) (snd(get_dims img)-1)) fileparse)::
	            (!x,!y,height_of_color (Sdlvideo.get_pixel_color img !x !y) fileparse) ::
		    (!x,snd(get_dims img)-1,height_of_color (Sdlvideo.get_pixel_color img !x (snd(get_dims img)-1)) fileparse) ::
		    (!x+inter,snd(get_dims img)-1,height_of_color (Sdlvideo.get_pixel_color img (!x+inter) (snd(get_dims img)-1)) fileparse) :: 
	            !liste;
	       x := !x + inter;
	       end 
	     end   
	 done;
  	 x := 0; (*on reinitialise x*)
	 y := !y + inter;	  
       done;
       !liste;
   end

 let rec doublons x = function
     [] -> false
   |e::l when x = e -> true
   |e::l -> doublons x l

 let rec tri = function
     [] -> []
   |e::l -> if (doublons e l) then (tri l) else e::(tri l)

 let rec cherche_ds_liste n e = function
     [] -> n
   |e1::l when e1 = e -> n
   |e1::l -> cherche_ds_liste (n+1) e l 

let rec moy = function
     [] -> 0
   |e::l -> (e + (moy l))/9


(*let bigmoy src x y = 
  begin
    if ((x > 0 && x < 512 && y > 0 && y < 512) && ((Sdlvideo.get_pixel_color src (x) (y) <> Sdlvideo.get_pixel_color src (x-1) (y-1)) || (Sdlvideo.get_pixel_color src (x) (y) <> Sdlvideo.get_pixel_color src (x-1) (y)) || ( Sdlvideo.get_pixel_color src (x) (y) <> Sdlvideo.get_pixel_color src (x-1) (y+1)) || (Sdlvideo.get_pixel_color src (x) (y) <> Sdlvideo.get_pixel_color src (x) (y-1)) || (Sdlvideo.get_pixel_color src (x) (y) <> Sdlvideo.get_pixel_color src (x) (y+1)) || (Sdlvideo.get_pixel_color src (x) (y) <> Sdlvideo.get_pixel_color src (x+1) (y-1)) || (Sdlvideo.get_pixel_color src (x) (y) <> Sdlvideo.get_pixel_color src (x+1) (y)) || (Sdlvideo.get_pixel_color src (x) (y) <> Sdlvideo.get_pixel_color src (x+1) (y+1)))) then
	let liste = ref [] in
	liste := [height_of_color(Sdlvideo.get_pixel_color src (x-1) (y-1));
	       	  height_of_color(Sdlvideo.get_pixel_color src (x-1) (y) );
		  height_of_color(Sdlvideo.get_pixel_color src (x-1) (y+1));
		  height_of_color(Sdlvideo.get_pixel_color src (x) (y-1));
		  height_of_color(Sdlvideo.get_pixel_color src (x) (y));
		  height_of_color(Sdlvideo.get_pixel_color src (x) (y+1));
		  height_of_color(Sdlvideo.get_pixel_color src (x+1) (y-1));
		  height_of_color(Sdlvideo.get_pixel_color src (x+1) (y));
		  height_of_color(Sdlvideo.get_pixel_color src (x+1) (y+1))];
moy !liste;
    else
      height_of_color(Sdlvideo.get_pixel_color src (x) (y) );
  end*)

 
 let list_to_obj img inter liste =
   begin
     let file = open_out "triangulation.obj" in
     let l = tri liste in
     let n = ref 0 in
     let f = function (x,y,h) -> output_string file ("v" ^ " " ^
						     string_of_int x ^
						     "." ^
						     " " ^
						     string_of_int y ^
						     "." ^
						     " " ^ 
						     string_of_int(h) ^
						     "." ^ "\n" ) in
       output_string file ("g triangulation.obj" ^ "\n" ^ "\n" );
       List.iter f l;
       output_string file ("\n");
       while !n <= (List.length liste) - 3 do
	 output_string file ("f" ^ " " ^ string_of_int(cherche_ds_liste 1 (List.nth liste !n) l) ^ "//" ^ " " ^
			     string_of_int (cherche_ds_liste 1 (List.nth liste (!n+1)) l) ^ "//" ^ " " ^
			     string_of_int (cherche_ds_liste 1 (List.nth liste (!n+2)) l) ^ "//" ^ "\n");
	   n := !n+3
	   
       done;
       close_out file;	        
   end
       


(* main *)
let main src interval listecouleur () =
  begin
    (* Nous voulons 1 argument *)
    (*if Array.length (Sys.argv) < 2 then
      failwith "Vous avez oublier le nom du fichier !";*)
    (* Initialisation de SDL *)
    sdl_init ();
    (* Chargement d'une image *)
    let img = Sdlloader.load_image src in
    (*print_string("Quelle intervalle voulez vous ?\n");*)
    let i = interval in
      
    (* On attend une touche *)
    list_to_obj img i (intersects (img) (i) listecouleur);
  end
